import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import dayjs from 'dayjs';
const { createConfirm } = useMessage();

const { createErrorModal } = useMessage();

enum Api {
  list = '/xl/xlCustomerOrde/list',
  save = '/xl/xlCustomerOrde/add',
  edit = '/xl/xlCustomerOrde/edit',
  getStoreInfoByOrderId = '/xl/xlCustomerOrde/getStoreInfoByOrderId',
  queryById = '/xl/xlCustomerOrde/queryById',
  audit = '/xl/xlCustomerOrde/audit',
  payment = '/xl/xlCustomerOrde/payment',
  shipment = '/xl/xlCustomerOrde/shipment',
  shouhuo = '/xl/xlCustomerOrde/shouhuo',
  submitPayment = '/xl/xlCustomerOrde/submitPayment',
  submitTakeDelivery = '/xl/xlCustomerOrde/submitTakeDelivery',
  submit = '/xl/xlCustomerOrde/submit',

  deleteOne = '/xl/xlCustomerOrde/delete',
  deleteBatch = '/xl/xlCustomerOrde/deleteBatch',
  importExcel = '/xl/xlCustomerOrde/importExcel',
  exportXls = '/xl/xlCustomerOrde/exportXls',
  xlCustomerOrderDetailList = '/xl/xlCustomerOrde/queryXlCustomerOrderDetailByMainId',
  productList = '/org/jeecg/modules/xl/xlProductInfo/list',
  downloadTemplateXls = '/xl/xlCustomerOrde/downloadTemplateXls',
  queryOrderDetailNotShipmentById = '/xl/xlCustomerOrde/queryOrderDetailNotShipmentById',

  exportPdfStream = '/jmreport/exportPdfStream',

  queryList = '/xl/xlCustomerOrde/queryOrderList2', ///xl/xlCustomerOrde/queryLis
  cityList = '/xl/xlCustomerOrde/cityList',
  cityOrderList = '/xl/xlCustomerOrde/cityOrderList',
  shiftDelivery = '/xl/xlCustomerOrde/shiftDelivery',
  handleCityShiftDelivery = '/xl/xlCustomerOrde/handleCityShiftDelivery',
  cityShipment = '/xl/xlCustomerOrde/cityShipment',
  handleRefreshLogistics = '/xl/xlCustomerOrde/handleRefreshLogistics',
  exportStatementPDF = '/xl/xlCustomerOrde/exportStatementPDF',
}
/**
 * 工单质量报表pdf下载
 * @param params
 */
export const exportStatementPDF = async (params) => {
  const data = await defHttp.pdfRequest({ url: Api.exportStatementPDF, params, responseType: 'blob', timeout: 20 * 1000 })
   let fileName  = params.receiptName +"-"+ String(params.totalRealAmount) +"对账单";
  return downloadPdf(fileName,data);
};
//下载
const downloadPdf = (name, bytes) => {
  // 指定 PDF MIME 类型
  const blob = new Blob([bytes], { type: 'application/pdf' });
  // 创建下载链接
  const link = document.createElement('a');
  link.href = URL.createObjectURL(blob);
  link.download = name + dayjs().format("HHmmss") + '.pdf'; // 设置下载文件名
  document.body.appendChild(link);
  link.click(); // 触发下载
  document.body.removeChild(link); // 移除链接

  // 释放 Blob URL
  URL.revokeObjectURL(link.href);
};
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 导入模板下载API
 */
export const downloadTemplateXlsUrl = Api.downloadTemplateXls;
/**
 * 查询子表数据
 * @param params
 */
export const xlCustomerOrderDetailList = Api.xlCustomerOrderDetailList;
 
/**
 * 查询子表数据接口
 * @param params
 */
export const xlCustomerOrderDetailList2 = (params) => defHttp.get({ url: Api.xlCustomerOrderDetailList, params });

/**
 * pda导出查询
 * @param params
 */
export const exportPdfStream = (params) => defHttp.pdfRequest({ url: Api.exportPdfStream, params, responseType: 'blob', timeout: 20 * 1000 });

export const copyToCreate = (params) => {
  return defHttp.post({ url: '/xl/xlCustomerOrde/copyToCreate', params });
};

export const createKMOrder = (params) => {
  return defHttp.get({ url: '/xl/xlCustomerOrde/createKMOrder', params }, { joinParamsToUrl: true });
};

export const createWDTOrder = (params) => {
  return defHttp.get({ url: '/xl/xlCustomerOrde/createWDTOrder', params }, { joinParamsToUrl: true });
};
/**
 * 子表接口
 * @param params
 */
export const queryOrderDetailNotShipmentById = (params) => defHttp.get({ url: Api.queryOrderDetailNotShipmentById, params });

/**
 * 城配列表接口
 * @param params
 */
export const cityList = (params) => defHttp.get({ url: Api.cityList, params });

/**
 * 城配订单接口
 * @param params
 */
export const cityOrderList = (params) => defHttp.get({ url: Api.cityOrderList, params });

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 订单查询接口
 * @param params
 */
export const queryList = (params) => defHttp.get({ url: Api.queryList, params });

/**
 * 商品列表数据
 * @param params
 */
export const productList = (params) => defHttp.get({ url: Api.productList, params });

/**
 * 根据订单id 门店信息查询
 * @param params
 */
export const getStoreInfoByOrderId = (params) => defHttp.get({ url: Api.getStoreInfoByOrderId, params });

/**
 * 采购订单详情
 * @param params
 */
export const queryById = (params) => defHttp.get({ url: Api.queryById, params });

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 审核
 * @param params
 */
export const audit = (params) => {
  return defHttp.post({ url: Api.audit, params });
};

/**
 * 付款
 * @param params
 */
export const payment = (params) => {
  return defHttp.post({ url: Api.payment, params });
};

/**
 * 发货
 * @param params
 */
export const shipment = (params) => {
  return defHttp.post({ url: Api.shipment, params });
};

/**
 * 收货
 * @param params
 */
export const shouhuo = (params) => {
  return defHttp.post({ url: Api.shouhuo, params });
};

/**
 * 支付凭证
 * @param params
 */
export const submitPayment = (params) => {
  return defHttp.post({ url: Api.submitPayment, params });
};
/**
 * 收货凭证
 * @param params
 */
export const submitTakeDelivery = (params) => {
  return defHttp.post({ url: Api.submitTakeDelivery, params });
};

/**
 * 提交
 * @param params
 */
export const sumbitOk = (params) => {
  // let resp = defHttp.post({ url: Api.submit, params });
  // console.log(resp);

  // return resp
  //return defHttp.post({ url: Api.submit, params });

  // return new Promise((resolve, reject) => {
  //   defHttp.post({ url: Api.submit, params }).then((res) => {
  //     console.log(res);
  //     if (true) {
  //       resolve(true);
  //     } else {
  //       createErrorModal({ title: '错误提示', content: res.message || '未知问题' });
  //       reject();
  //     }
  //   });
  // });
  return defHttp.post({ url: Api.submit, params });
};

/**
 * 一件代发
 * @param params
 */
export const shiftDelivery = (params) => {
  return defHttp.post({ url: Api.shiftDelivery, params });
};

/**
 * 删除单个
 */
export const cityShiftDelivery1 = (params, handleSuccess) => {
  return defHttp.post({ url: Api.handleCityShiftDelivery, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 城配发货
 * @param params
 */
export const cityShipment = (params) => {
  return defHttp.post({url: Api.cityShipment, params});
}

/**
 * 刷新物流
 * @param params
 */
export const handleRefreshLogistics = (params,handleSuccess) => {
  
  return defHttp.get({url: Api.handleRefreshLogistics, params}).then(()=>{
    handleSuccess();
  });
}